﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using MediaLibraryManagement.BLL;
using MediaLibraryManagement.Common;
using MediaLibraryManagement.Entity;

namespace MediaLibraryManagement.UI
{
    public partial class ArtistDetails : Form
    {
        private Artist _artist = null;

        public ArtistDetails()
        {
            InitializeComponent();
        }

        public ArtistDetails(Artist artist)
        {
            InitializeComponent();
            _artist = artist;
        }

        private void ArtistDetails_Load(object sender, EventArgs e)
        {
            if(_artist != null)
            {
                txtFirstName.Text = _artist.FirstName;
                txtLastName.Text = _artist.LastName;
                dtpDateOfBirth.Value = _artist.DateOfBirth;
                if (_artist.Gender == Gender.Male)
                {
                    cboGender.SelectedIndex = 0;
                }
                else
                {
                    cboGender.SelectedIndex = 1;
                }
                txtCountry.Text = _artist.Country;
                txtCompany.Text = _artist.Company;
                txtProfileURL.Text = _artist.ProfileURL;
                txtBiography.Text = _artist.Biography;
                picArtistImage.Image = _artist.Image;

                if(GlobalObjects.CurrentUser != null && GlobalObjects.CurrentUser.Privilege != Privileges.Viewer)
                {
                    SetEditingStatus(false);
                }
                else
                {
                    SetEditingStatus(true);
                    btnSave.Visible = false;
                }
            }
        }

        #region Private Methods
        private void SetEditingStatus(bool isReadOnly)
        {
            txtFirstName.ReadOnly = isReadOnly;
            txtLastName.ReadOnly = isReadOnly;
            dtpDateOfBirth.Enabled = !isReadOnly;
            txtCountry.ReadOnly = isReadOnly;
            txtCompany.ReadOnly = isReadOnly;
            txtProfileURL.ReadOnly = isReadOnly;
            txtBiography.ReadOnly = isReadOnly;
            btnBrowseImage.Enabled = !isReadOnly;
        }
        #endregion

        private void btnBrowseImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlgOpenFile = new OpenFileDialog();
            dlgOpenFile.Filter = GlobalObjects.ImageFilter;
            dlgOpenFile.Multiselect = false;
            if (dlgOpenFile.ShowDialog() == DialogResult.OK)
            {
                Image image = Image.FromFile(dlgOpenFile.FileName);
                Image newImage = Utilities.ScaleImage(image, picArtistImage.Width, picArtistImage.Height);
                picArtistImage.Image = newImage;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ArtistBLL business = new ArtistBLL();
            Artist artist = new Artist();
            artist.FirstName = txtFirstName.Text;
            artist.LastName = txtLastName.Text;
            artist.DateOfBirth = dtpDateOfBirth.Value;
            if(cboGender.SelectedIndex == 0)
            {
                artist.Gender = Gender.Male;
            }
            else if (cboGender.SelectedIndex == 1)
            {
                artist.Gender = Gender.Female;
            }
            artist.Country = txtCountry.Text;
            artist.Company = txtCompany.Text;
            artist.ProfileURL = txtProfileURL.Text;
            artist.Biography = txtBiography.Text;
            artist.Image = picArtistImage.Image;
            try
            {
                //Kiểm tra _artist đã tồn tại chưa.
                //Nếu tồn tại thì Update, nếu là null thì Insert.
                if (_artist == null)
                {
                    business.Insert(artist);
                    MessageBox.Show("Thêm nghệ sĩ mới thành công.");
                    this.Close();
                }
                else
                {
                    artist.ID = _artist.ID;
                    business.Update(artist);
                    MessageBox.Show("Cập nhật nghệ sĩ thành công.");
                    this.Close();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Lỗi hệ thống:\n" + ex, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lnkViewProfileURL_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if(!string.IsNullOrWhiteSpace(txtProfileURL.Text))
            {
                ProcessStartInfo startInfo = new ProcessStartInfo(txtProfileURL.Text);
                Process.Start(startInfo);
            }
        }
    }
}